package com.example.lt.config;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.example.lt.interceptor.JwtTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author 小涛Tao
 * @Date: 2024/05/30/8:43
 * version 1.0 注释：Spring Web MVC的配置类，配置自定义拦截器和跨域请求等
 **/
@Slf4j
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    /**
     * 自定义拦截路径
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/collect/user/**")
                .addPathPatterns("/comment/user/**")
                .addPathPatterns("/consumer/user/avatar", "/consumer/user/update", "/consumer/user/delete", "/consumer/user/recharge")
                .addPathPatterns("/rankList/user/**")
                .addPathPatterns("/userSupport/user/**")
                .addPathPatterns("/collect/admin/**")
                .addPathPatterns("/comment/admin/**")
                .addPathPatterns("/consumer/admin/**")
                .addPathPatterns("/listSong/admin/**")
                .addPathPatterns("/song/admin/**")
                .addPathPatterns("/songList/admin/**");
    }

    /**
     * 跨域请求配置
     * @param registry
     */
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 放行哪些原始域
                .allowedMethods("GET","POST","PUT","DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600)
                .exposedHeaders("*");
    }

    /**
     * 配置跨域资源共享（CORS）的过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setAllowCredentials(true);
        config.setMaxAge(3600L);
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }

}
